package com.hf.eagle.web.struts.admin.manager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.ModPopedom;
import com.hf.eagle.domain.SysModule;
import com.hf.eagle.web.base.BaseAction;

/**
 * @author Xu,XiaoYuan
 * @version 2011.8.16 23:45
 */
public class ModPopedomAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.edit(mapping, form, request, response);
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String user_id = (String) dynaBean.get("user_id");

		List<SysModule> entityList = new ArrayList<SysModule>();

		SysModule sysModule = new SysModule();
		sysModule.setPar_id(1l);
		sysModule.setIs_del(0);
		sysModule.getMap().put("is_not_admin", "true");
		List<SysModule> list = super.getFacade().getSysModuleService().getSysModuleList(sysModule);

		this.getSysModuleTreeList(entityList, list, 1);

		request.setAttribute("entityList", entityList);

		ModPopedom modPopedom = new ModPopedom();
		modPopedom.setUser_id(Long.valueOf(user_id));
		List<ModPopedom> modPopedomList = super.getFacade().getModPopedomService().getModPopedomList(modPopedom);
		request.setAttribute("modPopedomList", modPopedomList);

		dynaBean.set("queryString", super.serialize(request, "mod_id", "user_id", "method"));

		return mapping.findForward("input");
	}

	// 用户权限管理节点树的生成，需要有level，第一次传进来的list是根节点下经过排序的所有的文件和文件夹(不包括is_public=9)
	private void getSysModuleTreeList(List<SysModule> entityList, List<SysModule> smList, int level) {
		if (null != smList && smList.size() > 0) {
			for (SysModule sm : smList) {
				System.out.println("#############" + sm.getMod_name() + ":" + level);
				sm.getMap().put("level", level);
				if (StringUtils.isBlank(sm.getMod_url())) { // 文件夹
					entityList.add(sm);
					if (sm.getIs_public() == 0) { // 私有文件夹(此类文件夹下面全部为私有文件,全部取出)
						getSysModuleChildrenList(entityList, sm, level);
					} else { // 公共文件夹(此类文件夹可能含有私有文件,递归调用)
						SysModule sm_ = new SysModule();
						sm_.setPar_id(sm.getMod_id());
						List<SysModule> list = super.getFacade().getSysModuleService().getSysModuleList(sm_);

						int lev = level;
						if (null != list && list.size() > 0) {
							lev = level + 1;
						}

						getSysModuleTreeList(entityList, list, lev);
					}
				} else if (sm.getIs_public() == 0) { // 私有文件
					entityList.add(sm);
				}
			}
		}
	}

	private void getSysModuleChildrenList(List<SysModule> entityList, SysModule sm, int level) {
		SysModule sys_module = new SysModule();
		sys_module.setPar_id(sm.getMod_id());
		List<SysModule> list = super.getFacade().getSysModuleService().getSysModuleList(sys_module);
		if (null != list && list.size() > 0) {
			level++;
			for (SysModule sysModule : list) {
				sysModule.getMap().put("level", level);

				entityList.add(sysModule);

				getSysModuleChildrenList(entityList, sysModule, level);
			}
		}
	}

}
